{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 测试 byzerllm.emb 功能\n",
    "\n",
    "在这个notebook中，我们将测试byzerllm的emb（嵌入）功能。我们将展示如何使用byzerllm来生成文本的嵌入向量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n",
      "2024-08-26 12:49:30,016\tINFO util.py:154 -- Missing packages: ['ipywidgets']. Run `pip install -U ipywidgets`, then restart the notebook server for rich notebook output.\n"
     ]
    }
   ],
   "source": [
    "# 导入所需的库\n",
    "import byzerllm\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[32m2024-08-26 12:49:34.083\u001b[0m | \u001b[1mINFO    \u001b[0m | \u001b[36mbyzerllm.utils.connect_ray\u001b[0m:\u001b[36mconnect_cluster\u001b[0m:\u001b[36m48\u001b[0m - \u001b[1mJDK 21 will be used (/Users/allwefantasy/.auto-coder/jdk-21.0.2.jdk/Contents/Home)...\u001b[0m\n",
      "2024-08-26 12:49:34,128\tINFO worker.py:1596 -- Connecting to existing Ray cluster at address: 127.0.0.1:6379...\n",
      "2024-08-26 12:49:34,141\tINFO worker.py:1772 -- Connected to Ray cluster. View the dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8265 \u001b[39m\u001b[22m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<byzerllm.utils.client.byzerllm_client.ByzerLLM at 0x137100110>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化ByzerLLM\n",
    "byzerllm.ByzerLLM.from_default_model(\"deepseek_chat\")\n",
    "llm = byzerllm.ByzerLLM()\n",
    "\n",
    "# 设置默认的嵌入模型\n",
    "# 注意：请确保您已经部署了相应的嵌入模型，例如 \"emb\"\n",
    "llm.setup_default_emb_model_name(\"emb\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-08-26 13:26:13,107\tERROR serialization.py:425 -- Failed to unpickle serialized exception\n",
      "Traceback (most recent call last):\n",
      "  File \"/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/exceptions.py\", line 50, in from_ray_exception\n",
      "    return pickle.loads(ray_exception.serialized_exception)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "TypeError: APIStatusError.__init__() missing 2 required keyword-only arguments: 'response' and 'body'\n",
      "\n",
      "The above exception was the direct cause of the following exception:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/_private/serialization.py\", line 423, in deserialize_objects\n",
      "    obj = self._deserialize_object(data, metadata, object_ref)\n",
      "          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/_private/serialization.py\", line 305, in _deserialize_object\n",
      "    return RayError.from_bytes(obj)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/exceptions.py\", line 44, in from_bytes\n",
      "    return RayError.from_ray_exception(ray_exception)\n",
      "           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
      "  File \"/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/exceptions.py\", line 53, in from_ray_exception\n",
      "    raise RuntimeError(msg) from e\n",
      "RuntimeError: Failed to unpickle serialized exception\n"
     ]
    },
    {
     "ename": "RaySystemError",
     "evalue": "System error: Failed to unpickle serialized exception\ntraceback: Traceback (most recent call last):\n  File \"/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/exceptions.py\", line 50, in from_ray_exception\n    return pickle.loads(ray_exception.serialized_exception)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nTypeError: APIStatusError.__init__() missing 2 required keyword-only arguments: 'response' and 'body'\n\nThe above exception was the direct cause of the following exception:\n\nTraceback (most recent call last):\n  File \"/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/_private/serialization.py\", line 423, in deserialize_objects\n    obj = self._deserialize_object(data, metadata, object_ref)\n          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/_private/serialization.py\", line 305, in _deserialize_object\n    return RayError.from_bytes(obj)\n           ^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/exceptions.py\", line 44, in from_bytes\n    return RayError.from_ray_exception(ray_exception)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/exceptions.py\", line 53, in from_ray_exception\n    raise RuntimeError(msg) from e\nRuntimeError: Failed to unpickle serialized exception\n",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mRaySystemError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[7], line 3\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# 测试单个文本的嵌入\u001b[39;00m\n\u001b[1;32m      2\u001b[0m text \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mHello, world!\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 3\u001b[0m embedding \u001b[38;5;241m=\u001b[39m \u001b[43mllm\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43memb_query\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m      5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mText: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtext\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m      6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEmbedding shape: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mnp\u001b[38;5;241m.\u001b[39marray(embedding)\u001b[38;5;241m.\u001b[39mshape\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[0;32m~/projects/byzer-llm/src/byzerllm/utils/client/byzerllm_client.py:728\u001b[0m, in \u001b[0;36mByzerLLM.emb_query\u001b[0;34m(self, v, model)\u001b[0m\n\u001b[1;32m    727\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21memb_query\u001b[39m(\u001b[38;5;28mself\u001b[39m, v: \u001b[38;5;28mstr\u001b[39m, model: \u001b[38;5;28mstr\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[0;32m--> 728\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43memb\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrequest\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mLLMRequest\u001b[49m\u001b[43m(\u001b[49m\u001b[43minstruction\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mv\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/projects/byzer-llm/src/byzerllm/utils/client/byzerllm_client.py:768\u001b[0m, in \u001b[0;36mByzerLLM.emb\u001b[0;34m(self, model, request, extract_params)\u001b[0m\n\u001b[1;32m    755\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    756\u001b[0m     v \u001b[38;5;241m=\u001b[39m [\n\u001b[1;32m    757\u001b[0m         {\n\u001b[1;32m    758\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minstruction\u001b[39m\u001b[38;5;124m\"\u001b[39m: x,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    766\u001b[0m         \u001b[38;5;28;01mfor\u001b[39;00m x \u001b[38;5;129;01min\u001b[39;00m request\u001b[38;5;241m.\u001b[39minstruction\n\u001b[1;32m    767\u001b[0m     ]\n\u001b[0;32m--> 768\u001b[0m res \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_query\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mv\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    770\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m [\n\u001b[1;32m    771\u001b[0m     LLMResponse(\n\u001b[1;32m    772\u001b[0m         output\u001b[38;5;241m=\u001b[39mitem[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpredict\u001b[39m\u001b[38;5;124m\"\u001b[39m],\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    776\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m item \u001b[38;5;129;01min\u001b[39;00m res\n\u001b[1;32m    777\u001b[0m ]\n",
      "File \u001b[0;32m~/projects/byzer-llm/src/byzerllm/utils/client/byzerllm_client.py:1882\u001b[0m, in \u001b[0;36mByzerLLM._query\u001b[0;34m(self, model, input_value)\u001b[0m\n\u001b[1;32m   1880\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m json\u001b[38;5;241m.\u001b[39mloads(res[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m\"\u001b[39m][\u001b[38;5;241m0\u001b[39m])\n\u001b[1;32m   1881\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m inst:\n\u001b[0;32m-> 1882\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m inst\n\u001b[1;32m   1883\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m   1884\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m index \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m:\n",
      "File \u001b[0;32m~/projects/byzer-llm/src/byzerllm/utils/client/byzerllm_client.py:1872\u001b[0m, in \u001b[0;36mByzerLLM._query\u001b[0;34m(self, model, input_value)\u001b[0m\n\u001b[1;32m   1869\u001b[0m         worker_id \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mpin_model_worker_mapping\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmeta\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m)\n\u001b[1;32m   1871\u001b[0m [index, worker] \u001b[38;5;241m=\u001b[39m ray\u001b[38;5;241m.\u001b[39mget(udf_master\u001b[38;5;241m.\u001b[39mget\u001b[38;5;241m.\u001b[39mremote(worker_id))\n\u001b[0;32m-> 1872\u001b[0m res \u001b[38;5;241m=\u001b[39m \u001b[43mray\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[43mworker\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43masync_apply\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremote\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnew_input_value\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1874\u001b[0m event_result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_trigger_event(\n\u001b[1;32m   1875\u001b[0m     EventName\u001b[38;5;241m.\u001b[39mAFTER_CALL_MODEL, \u001b[38;5;28mself\u001b[39m, model, json\u001b[38;5;241m.\u001b[39mloads(res[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvalue\u001b[39m\u001b[38;5;124m\"\u001b[39m][\u001b[38;5;241m0\u001b[39m])\n\u001b[1;32m   1876\u001b[0m )\n\u001b[1;32m   1877\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m event_result \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
      "File \u001b[0;32m/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/_private/auto_init_hook.py:21\u001b[0m, in \u001b[0;36mwrap_auto_init.<locals>.auto_init_wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m     18\u001b[0m \u001b[38;5;129m@wraps\u001b[39m(fn)\n\u001b[1;32m     19\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mauto_init_wrapper\u001b[39m(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m     20\u001b[0m     auto_init_ray()\n\u001b[0;32m---> 21\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/_private/client_mode_hook.py:103\u001b[0m, in \u001b[0;36mclient_mode_hook.<locals>.wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m    101\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m func\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m!=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minit\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mor\u001b[39;00m is_client_mode_enabled_by_default:\n\u001b[1;32m    102\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mgetattr\u001b[39m(ray, func\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m)(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m--> 103\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/_private/worker.py:2659\u001b[0m, in \u001b[0;36mget\u001b[0;34m(object_refs, timeout)\u001b[0m\n\u001b[1;32m   2653\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m   2654\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid type of object refs, \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(object_refs)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, is given. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   2655\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mobject_refs\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m must either be an ObjectRef or a list of ObjectRefs. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   2656\u001b[0m     )\n\u001b[1;32m   2658\u001b[0m \u001b[38;5;66;03m# TODO(ujvl): Consider how to allow user to retrieve the ready objects.\u001b[39;00m\n\u001b[0;32m-> 2659\u001b[0m values, debugger_breakpoint \u001b[38;5;241m=\u001b[39m \u001b[43mworker\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_objects\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobject_refs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtimeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtimeout\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   2660\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m i, value \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28menumerate\u001b[39m(values):\n\u001b[1;32m   2661\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(value, RayError):\n",
      "File \u001b[0;32m/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/_private/worker.py:873\u001b[0m, in \u001b[0;36mWorker.get_objects\u001b[0;34m(self, object_refs, timeout, return_exceptions)\u001b[0m\n\u001b[1;32m    871\u001b[0m                 \u001b[38;5;28;01mraise\u001b[39;00m value\u001b[38;5;241m.\u001b[39mas_instanceof_cause()\n\u001b[1;32m    872\u001b[0m             \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 873\u001b[0m                 \u001b[38;5;28;01mraise\u001b[39;00m value\n\u001b[1;32m    875\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m values, debugger_breakpoint\n",
      "\u001b[0;31mRaySystemError\u001b[0m: System error: Failed to unpickle serialized exception\ntraceback: Traceback (most recent call last):\n  File \"/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/exceptions.py\", line 50, in from_ray_exception\n    return pickle.loads(ray_exception.serialized_exception)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nTypeError: APIStatusError.__init__() missing 2 required keyword-only arguments: 'response' and 'body'\n\nThe above exception was the direct cause of the following exception:\n\nTraceback (most recent call last):\n  File \"/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/_private/serialization.py\", line 423, in deserialize_objects\n    obj = self._deserialize_object(data, metadata, object_ref)\n          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/_private/serialization.py\", line 305, in _deserialize_object\n    return RayError.from_bytes(obj)\n           ^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/exceptions.py\", line 44, in from_bytes\n    return RayError.from_ray_exception(ray_exception)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/opt/miniconda3/envs/byzer-test/lib/python3.11/site-packages/ray/exceptions.py\", line 53, in from_ray_exception\n    raise RuntimeError(msg) from e\nRuntimeError: Failed to unpickle serialized exception\n"
     ]
    }
   ],
   "source": [
    "# 测试单个文本的嵌入\n",
    "text = \"Hello, world!\"\n",
    "embedding = llm.emb_query([None])\n",
    "\n",
    "print(f\"Text: {text}\")\n",
    "print(f\"Embedding shape: {np.array(embedding).shape}\")\n",
    "print(f\"First few values of embedding: {embedding[:5]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mRunning cells with 'ai-vocabulary-builder (Python 3.10.9)' requires the ipykernel package.\n",
      "\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n",
      "\u001b[1;31mCommand: 'conda install -n ai-vocabulary-builder ipykernel --update-deps --force-reinstall'"
     ]
    }
   ],
   "source": [
    "# 测试多个文本的嵌入\n",
    "texts = [\"Hello, world!\", \"ByzerLLM is awesome\", \"Embedding models are useful\"]\n",
    "embeddings = llm.emb_query_batch(texts)\n",
    "\n",
    "for i, (text, embedding) in enumerate(zip(texts, embeddings)):\n",
    "    print(f\"Text {i+1}: {text}\")\n",
    "    print(f\"Embedding shape: {np.array(embedding).shape}\")\n",
    "    print(f\"First few values of embedding: {embedding[:5]}\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mRunning cells with 'ai-vocabulary-builder (Python 3.10.9)' requires the ipykernel package.\n",
      "\u001b[1;31mRun the following command to install 'ipykernel' into the Python environment. \n",
      "\u001b[1;31mCommand: 'conda install -n ai-vocabulary-builder ipykernel --update-deps --force-reinstall'"
     ]
    }
   ],
   "source": [
    "# 计算两个嵌入之间的余弦相似度\n",
    "def cosine_similarity(a, b):\n",
    "    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))\n",
    "\n",
    "similarity = cosine_similarity(embeddings[0], embeddings[1])\n",
    "print(f\"Cosine similarity between '{texts[0]}' and '{texts[1]}': {similarity}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这个notebook展示了如何使用byzerllm的emb功能来生成文本的嵌入向量。我们测试了单个文本和多个文本的嵌入，并展示了如何计算嵌入之间的相似度。\n",
    "\n",
    "请注意，在运行这个notebook之前，确保您已经正确设置和部署了嵌入模型。如果遇到任何问题，请检查您的byzerllm配置和模型部署状态。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
